﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Devamigo.Core
{
    public class TestRepository : IRepository
    {
        private Dictionary<Type, IList> _dataStore = null;
        private Dictionary<Type, IList> _insertTransactions = null;
        private Dictionary<Type, IList> _deleteTransactions = null;

        public TestRepository() 
        {
            _dataStore = new Dictionary<Type, IList>();
            _insertTransactions = new Dictionary<Type, IList>();
            _deleteTransactions = new Dictionary<Type, IList>();
        }

        #region IRepository Members

        public IEnumerable<TModel> Query<TModel>() 
            where TModel : class
        {
            Type entityType = typeof(TModel);

            if (!_dataStore.ContainsKey(entityType))
            {
                _dataStore.Add(entityType, new List<TModel>());
            }

            return (IEnumerable<TModel>)_dataStore[entityType];
        }

        public void InsertOnSubmit<TModel>(TModel entity) 
            where TModel : class
        {
            Type entityType = typeof(TModel);

            if (!_dataStore.ContainsKey(entityType))
            {
                _dataStore.Add(entityType, new List<TModel>());
            }

            if (!_insertTransactions.ContainsKey(entityType))
            {
                _insertTransactions.Add(entityType, new List<TModel>());
            }

            _insertTransactions[entityType].Add(entity);
        }

        public void InsertOnSubmit<TModel>(IEnumerable<TModel> entities) 
            where TModel : class
        {
            foreach (var entity in entities)
            {
                InsertOnSubmit<TModel>(entity);
            }
        }

        public void DeleteOnSubmit<TModel>(TModel entity) 
            where TModel : class
        {
            Type entityType = typeof(TModel);

            if (!_dataStore.ContainsKey(entityType))
            {
                _dataStore.Add(entityType, new List<TModel>());
            }

            if (!_deleteTransactions.ContainsKey(entityType))
            {
                _deleteTransactions.Add(entityType, new List<TModel>());
            }

            _deleteTransactions[entityType].Add(entity);
        }

        public void DeleteOnSubmit<TModel>(IEnumerable<TModel> entities) where TModel : class
        {
            foreach (var entity in entities)
            {
                DeleteOnSubmit<TModel>(entity);
            }
        }

        public void SubmitChanges()
        {
            // insert entities
            foreach (Type entityType in _insertTransactions.Keys)
            {
                foreach (object obj in _insertTransactions[entityType])
                {
                    _dataStore[entityType].Add(obj);
                }
            }
            _insertTransactions.Clear();

            // delete entities
            foreach (Type entityType in _deleteTransactions.Keys)
            {
                foreach (object obj in _deleteTransactions[entityType])
                {
                    if (_dataStore[entityType].Contains(obj))
                    {
                        _dataStore[entityType].Remove(obj);
                    }
                }
            }
            _deleteTransactions.Clear();
        }

        #endregion
    }
}
